﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using CoolCode.Services;

namespace CoolCode.Web
{
    public static class ServiceExtensions
    {

        public static void AsExportable<T>(this IButtonControl control, IExportService<T> exportService, Func<List<T>> provideData,
            Action<ExportResponse> success, Action<ExportResponse> fail)
        {
            control.Click += (sender, e) =>
            {
                ExportResult result;
                var list = provideData();
                string filePath = exportService.Export(list, out result);
                if (result == ExportResult.Success)
                {
                    success(new ExportResponse(result, filePath));
                }
                else
                {
                    fail(new ExportResponse(result, filePath));
                }
            };
        }

    }

    public class ExportResponse
    {
        public ExportResult Result { get; private set; }
        public string FilePath { get; private set; }

        public string Message { get { return this.Result.GetAttachedString(); } }

        public ExportResponse(ExportResult result, string filePath)
        {
            this.Result = result;
            this.FilePath = filePath;
        }
    }
}
